package reformatNumber;

import java.util.ArrayList;
import java.util.List;
class Solution {
    public String reformatNumber(String number) {
        List<Character> list = new ArrayList<>();
        for (int i = 0; i < number.length(); i++) {
            if('0'<=number.charAt(i)&&number.charAt(i)<='9'){
                list.add(number.charAt(i));
            }
        }
        int n = list.size();
        int three,two;
        if(n%3==0){
            three = n/3;
            two = 0;
        } else if (n%3 == 2) {
            three = n/3;
            two = 1;
        } else {
            three = (n-4)/3;
            two = 2;
        }
        StringBuilder sb = new StringBuilder();
        int i = 0;
        while (three>0){
            for (int j = 0; j < 3; j++) {
                sb.append(list.get(i));
                i++;
            }
            sb.append('-');
            three--;
        }
        while (two>0){
            for (int j = 0; j < 2; j++) {
                sb.append(list.get(i));
                i++;
            }
            sb.append("-");
            two--;
        }
        // 移除最后一个破折号
        return sb.toString().substring(0,sb.length()-1);
    }
}
